package controller;

import bean.Person;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;

@Controller
// @WebServlet("/first")
// 类似于@WebServlet的一个注解，对那些请求拦截，可以修饰类或者方法
@RequestMapping("/first")
public class FirstController {

    // 接收请求，做出响应
    // http://localhost:8080/mvc_xml_war/first/a
    @RequestMapping("/a")
    public String a() {
        // 请求
        System.out.println("First类的a方法接收了用户的请求");
        // 响应  跳转
        return "index";
    }

    /*
    value:路径
    method:接收那些类型请求
    produces:响应的编码集和类型
     */
    @RequestMapping(value = "/b", method = RequestMethod.GET, produces = {"text/html;charset=utf-8"})
    public String b() {

        return "index";
    }

    // 只要接收的参数名和形参列表一致，自动装配(自动映射)
    @RequestMapping("/c")
    public String c(String name, String pass, int age, String[] loves) {
        System.out.println(name);
        System.out.println(pass);
        System.out.println(age);
        System.out.println(Arrays.toString(loves));
        return "index";
    }

    // 手动映射
    @RequestMapping("/d")
    public String d(@RequestParam("pass") String password, @RequestParam(value = "age", defaultValue = "18") int userAge) {
        System.out.println(password);
        System.out.println(userAge);
        return "index";
    }

    // 自动封装
    @RequestMapping("/e")
    public String e(Person person) {
        System.out.println(person);
        return "index";
    }

    // 重定向
    @RequestMapping("/f")
    public String f() {
        System.out.println("重定向");
        // 重定向和视图解析器无关
        return "redirect:/index.jsp";
    }

    // 获取 ServletAPI
    @RequestMapping(value = "/g", produces = {"text/html;charset=utf-8"})
    public String g(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException {
        PrintWriter out = response.getWriter();
        ServletContext application = session.getServletContext();
        request.setAttribute("A", "FirstController`g方法存放的request内容");
        session.setAttribute("B", "FirstController`g方法存放的session内容");
        application.setAttribute("C", "FirstController`g方法存放的application内容");
        return "2";
    }

    @RequestMapping("/h")
    public ModelAndView h(ModelAndView mv) {
        // 模型(request)
        mv.getModel().put("A", "model存放的内容");
        mv.setViewName("3");
        return mv;
    }

    // 通过 out 对象输出内容
    @RequestMapping(value = "/i1", produces = {"plain/text;charset=utf-8"})
    public void i1(HttpServletResponse response) throws IOException {
        PrintWriter out = response.getWriter();
        out.println("This is FirstController`i1 method!");
    }

    // 缩写形式 通过一个注解 @ResponseBody
    @ResponseBody
    @RequestMapping("/i2")
    public String i2(){
        return "This is FirstController`i2 method!";
    }
}
